package cn.net.msg.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import cn.net.msg.dao.MemberDao;
import cn.net.msg.model.ReturnPointBean;
import cn.net.msg.model.TbFavourite;
import cn.net.msg.model.TbUser;
import cn.net.msg.service.base.DaoSupport;
import cn.net.msg.service.base.Order;
import cn.net.msg.utils.MD5;

@Component("memberDao")
public class MemberDaoImpl extends DaoSupport implements MemberDao {

    @Override
    public TbUser checkLogin(String userNameOrEmail, String password) {
        // TODO Auto-generated method stub
        String hql = "select t from TbUser t where (t.userName = ? or t.userEmail = ?) and t.userPassword = ?";
        List<Object> params = new ArrayList<Object>();
        params.add(userNameOrEmail);
        params.add(userNameOrEmail);
        params.add(password);
        TbUser tbUser = (TbUser)super.executeSacale(hql, params);
        return tbUser;
    }

    @Override
    public void updateLoginTime(TbUser tbUser) {
        // TODO Auto-generated method stub
        String hql = "update TbUser u set u.userLogintime = NOW() where (u.userName = ? or u.userEmail = ?) and u.userPassword = ?";
        List<Object> params = new ArrayList<Object>();
        params.add(tbUser.getUserName());
        params.add(tbUser.getUserEmail());
        params.add(tbUser.getUserPassword());
        super.executNonQuery(hql, params);
    }

    @Override
    public boolean isExsitName(String name) {
        String hql = "select t from TbUser t where t.userName = ?";
        List<Object> params = new ArrayList<Object>();
        params.add(name);
        TbUser tbUser = (TbUser)super.executeSacale(hql, params);
        return tbUser != null;
    }

    @Override
    public boolean isExsitEmail(String email){
        String hql = "select t from TbUser t where t.userEmail = ?";
        List<Object> params = new ArrayList<Object>();
        params.add(email);
        TbUser tbUser = (TbUser)super.executeSacale(hql, params);
        return tbUser != null;
    }

    @Override
    public void saveUser(TbUser tbUser) {
        // TODO Auto-generated method stub
        super.save(tbUser);
    }

    @Override
    public ReturnPointBean getUserPoint(long userid) {
        // TODO Auto-generated method stub
        String hql = "select new Map(u.userPoint as userPoint,g.gradeName as gradeName) from TbUser u,TbGrade g where g.gradeId = u.gradeId and u.userId = ?";
        List<Object> params = new ArrayList<Object>();
        params.add(userid);
        Map map = (Map)super.executeSacale(hql, params);
        ReturnPointBean returnPointBean = new ReturnPointBean();
        returnPointBean.setUserPoint((Integer)map.get("userPoint"));
        returnPointBean.setGradeName((String)map.get("gradeName"));
        return returnPointBean;        
    }

    @Override
    public int deleteUser(long id) {
        // TODO Auto-generated method stub
        String hql_1 = "delete TbScore s where s.userId = ?";
        List<Object> params_1 = new ArrayList<Object>();
        params_1.add(id);
        int user = super.executNonQuery(hql_1, params_1);

        String hql_2 = "delete TbUser u where u.userId = ?";
        List<Object> params_2 = new ArrayList<Object>();
        params_2.add(id);
        int score = super.executNonQuery(hql_2, params_2);

        return user + score;
    }

    @Override
    public TbUser find(long userid) {
        // TODO Auto-generated method stub
        return super.query(TbUser.class, userid);
    }

    @Override
    public void update(TbUser tbUser) {
        // TODO Auto-generated method stub
        super.update(tbUser);
    }

    @Override
    public void uploadUserPoints(Map<String, Object> map) {
        // TODO Auto-generated method stub
        String hql = "update TbUser u set u.userPoint = ?,u.gradeId = ? where u.userId = ?";
        List<Object> params = new ArrayList<Object>();
        params.add(map.get("userPoint"));
        int userpoint = Integer.parseInt(map.get("userPoint").toString()); 
        if (userpoint >= 0 && userpoint < 200) {
            params.add(1);
        } else if (userpoint >= 200 && userpoint < 400) {
            params.add(2);
        } else if (userpoint >= 400 && userpoint < 600) {
            params.add(3);
        } else if (userpoint >= 600 && userpoint < 800) {
            params.add(4);
        } else if (userpoint >= 800) {
            params.add(5);
        }
        params.add(map.get("userId"));
        super.executNonQuery(hql, params);
    }

    @Override
    public void updateUserPassword(String email, String password) {
        // TODO Auto-generated method stub
        String hql = "update TbUser u set u.userPassword = ? where u.userEmail = ?";
        List<Object> params = new ArrayList<Object>();
        params.add(MD5.MD5Encode(password));
        params.add(email);
        super.executNonQuery(hql, params);
    }

    @Override
    public List<TbFavourite> getUserFavouriteForList(int firstIndex,
            int maxResult, List<HashMap<String, Object>> queryParams,
            Order order, String propertyName) {
        // TODO Auto-generated method stub
        return super.paging(TbFavourite.class, firstIndex, maxResult, queryParams, order, propertyName);
    }
}
